const BaseHandler = require('../base-handler');
const SystemServices = require('../../../constants').SystemServices;
const HTTP_VERBS = require('../../../constants').HTTP_VERBS;

class DashboardDataHandler extends BaseHandler {
    get pattern() {
        return "/dashboard/:type";
    }

    doGET(request, response) {
        if (!request.session.user.authenticated) {
            response.json({
                success: false,
                error: '请登录系统后再调用此接口！'
            });
        } else {
            let type = request.params.type;
            let params = { storeId: request.session.user.storeId, type, startTime: request.query.startTime, endTime: request.query.endTime };
            this.Controller.getOccupyData(params, (result) => {
                if (result.error) {
                    return response.json({
                        success: false,
                        error: result.error
                    });
                }
                if (type == 'total') {
                    result.data = [
                        { name: 'VIP客户', count: (result.data.find(item => { return item.isvip == 'vip' })).count },
                        { name: '普通客户', count: (result.data.find(item => { return item.isvip == 'guest' })).count }
                        // { name: 'VIP客户', count: result.data.vipCount },
                        // { name: '普通客户', count: result.data.guestCount }
                    ]
                } else if (type == 'gender') {
                    result.data = [
                        { name: '男', count: (result.data.find(item => { return item.gender == '男' })).count },
                        { name: '女', count: (result.data.find(item => { return item.gender == '女' })).count },
                        { name: '未知', count: (result.data.find(item => { return item.gender == '未知' })).count }
                    ]
                }

                response.json({
                    success: true,
                    result: result
                });
            })

        }

    }

    doPOST(request, response) {

    }

    setup() {
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
        this.registerVerb(HTTP_VERBS.POST, this.doPOST)
    }
}

module.exports = DashboardDataHandler;